<template>
    <div class="notifications-container">
      <div class="notifications-header">
        <h1>通知中心</h1>
        <div class="header-actions">
          <button class="refresh-btn" @click="refreshNotifications">
            <i class="iconfont icon-refresh"></i>
            刷新
          </button>
        </div>
      </div>

      <div class="notifications-list" ref="notificationsList" @scroll="handleScroll">
        <div v-if="loading && notifications.length === 0" class="loading-placeholder">
          <div class="spinner"></div>
          <p>加载中...</p>
        </div>

        <div v-else-if="notifications.length === 0" class="empty-state">
          <i class="iconfont icon-notification"></i>
          <p>暂无通知</p>
        </div>

        <div v-else>
          <div
            v-for="(notification, index) in notifications"
            :key="index"
            class="notification-item"
            :class="{ unread: !notification.read }"
          >
            <div class="notification-content">
              <div class="notification-message">{{ notification.message }}</div>
              <div class="notification-time">{{ formatTime(notification.receiveTime) }}</div>
            </div>
          </div>

          <div v-if="loading && notifications.length > 0" class="loading-more">
            <div class="spinner small"></div>
            <span>加载更多通知...</span>
          </div>

          <div v-if="noMoreData && notifications.length > 0" class="no-more-data">
            <span>没有更多通知了</span>
          </div>
        </div>
      </div>
    </div>
  </template>

<script>
import { ref, onMounted } from 'vue'
import apiClient from '@/utils/api'

export default {
  name: 'Notifications',
  setup () {
    const notifications = ref([])
    const loading = ref(false)
    const noMoreData = ref(false)
    const offset = ref(0)
    const notificationsList = ref(null)

    // 格式化时间
    const formatTime = (timeString) => {
      const date = new Date(timeString)
      const now = new Date()
      const diffTime = Math.abs(now - date)
      const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))

      if (diffDays === 1) {
        return '昨天 ' + date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })
      } else if (diffDays <= 7) {
        const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
        return weekdays[date.getDay()] + ' ' + date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })
      } else {
        return date.toLocaleString('zh-CN', {
          year: 'numeric',
          month: '2-digit',
          day: '2-digit',
          hour: '2-digit',
          minute: '2-digit'
        })
      }
    }

    // 获取通知列表
    const fetchNotifications = async () => {
      if (loading.value || noMoreData.value) return

      loading.value = true
      try {
        const response = await apiClient.get('/chat/getUserInformRecord', {
          params: {
            offset: offset.value
          }
        })

        if (response.data.code === '200') {
          const newNotifications = response.data.data

          if (newNotifications.length === 0) {
            noMoreData.value = true
          } else {
            notifications.value = [...notifications.value, ...newNotifications]
            offset.value += newNotifications.length
          }
        } else {
          console.error('获取通知失败:', response.data.msg)
        }
      } catch (error) {
        console.error('获取通知失败:', error)
      } finally {
        loading.value = false
      }
    }

    // 刷新通知
    const refreshNotifications = async () => {
      notifications.value = []
      offset.value = 0
      noMoreData.value = false
      await fetchNotifications()
    }

    // 处理滚动事件
    const handleScroll = () => {
      if (!notificationsList.value || loading.value || noMoreData.value) return

      const { scrollTop, scrollHeight, clientHeight } = notificationsList.value
      if (scrollTop + clientHeight >= scrollHeight - 10) {
        fetchNotifications()
      }
    }

    // 初始化加载
    onMounted(() => {
      fetchNotifications()
    })

    return {
      notifications,
      loading,
      noMoreData,
      notificationsList,
      formatTime,
      fetchNotifications,
      refreshNotifications,
      handleScroll
    }
  }
}
</script>

  <style scoped>
  .notifications-container {
    max-width: 800px;
    margin: 0 auto;
    padding: 20px;
    background-color: #ffffff;
    min-height: 100vh;
    box-sizing: border-box;
  }

  .notifications-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    padding-bottom: 16px;
    border-bottom: 1px solid #e8e8e8;
  }

  .notifications-header h1 {
    color: #1890ff;
    font-size: 24px;
    font-weight: 600;
    margin: 0;
  }

  .header-actions .refresh-btn {
    background-color: #1890ff;
    color: white;
    border: none;
    border-radius: 4px;
    padding: 8px 16px;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
    transition: background-color 0.3s;
  }

  .header-actions .refresh-btn:hover {
    background-color: #40a9ff;
  }

  .notifications-list {
    height: calc(100vh - 140px);
    overflow-y: auto;
    padding-right: 8px;
  }

  .notifications-list::-webkit-scrollbar {
    width: 6px;
  }

  .notifications-list::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 10px;
  }

  .notifications-list::-webkit-scrollbar-thumb {
    background: #1890ff;
    border-radius: 10px;
  }

  .notifications-list::-webkit-scrollbar-thumb:hover {
    background: #40a9ff;
  }

  .notification-item {
    background-color: #fafafa;
    border-radius: 8px;
    padding: 16px;
    margin-bottom: 12px;
    transition: all 0.3s;
    border-left: 4px solid #1890ff;
  }

  .notification-item:hover {
    background-color: #f0f8ff;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(24, 144, 255, 0.1);
  }

  .notification-item.unread {
    background-color: #e6f7ff;
    border-left: 4px solid #ff4d4f;
  }

  .notification-content .notification-message {
    color: #333;
    font-size: 16px;
    line-height: 1.5;
    margin-bottom: 8px;
    word-wrap: break-word;
  }

  .notification-content .notification-time {
    color: #888;
    font-size: 12px;
    text-align: right;
  }

  .loading-placeholder,
  .empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 300px;
    color: #888;
  }

  .empty-state i {
    font-size: 48px;
    margin-bottom: 16px;
    color: #1890ff;
  }

  .spinner {
    width: 40px;
    height: 40px;
    border: 4px solid #f3f3f3;
    border-top: 4px solid #1890ff;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 16px;
  }

  .spinner.small {
    width: 20px;
    height: 20px;
    border-width: 2px;
    margin-bottom: 0;
    margin-right: 8px;
  }

  @keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
  }

  .loading-more {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    color: #888;
  }

  .no-more-data {
    text-align: center;
    padding: 24px;
    color: #888;
    font-size: 14px;
  }

  /* 响应式设计 */
  @media (max-width: 768px) {
    .notifications-container {
      padding: 16px;
    }

    .notifications-header {
      flex-direction: column;
      align-items: flex-start;
      gap: 16px;
    }

    .header-actions {
      align-self: flex-end;
    }

    .notification-content .notification-message {
      font-size: 14px;
    }
  }
  </style>
